﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace CheckMemcached
{
    public class MemcachedServer : ConfigurationSection
    {
        [ConfigurationProperty("Memcached")]
        public Memcached Memcached
        {
            get { return this["Memcached"] as Memcached; }
            set { this["Memcached"] = value; }
        }
    }
    public class Memcached : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new Server();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            Server server = element as Server;
            return server.Host;
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }
        protected override string ElementName
        {
            get
            {
                return "Server";
            }
        }
    }
    public class Server : ConfigurationElement
    {
        /// <summary>
        /// 主机地址
        /// </summary>
        [ConfigurationProperty("Host", IsRequired = true, IsKey = true)]
        public string Host
        {
            get { return this["Host"] as string; }
            set { this["Host"] = value; }
        }
        /// <summary>
        /// 端口
        /// </summary>
        [ConfigurationProperty("Port", IsRequired = true)]
        public int Port
        {
            get { return Convert.ToInt32(this["Port"]); }
            set { this["Port"] = value; }
        }

        /// <summary>
        /// 是否本地服务
        /// </summary>
        [ConfigurationProperty("IsLocalService", IsRequired = true)]
        public bool IsLocalService
        {
            get { return this["IsLocalService"].ToString().ToLower() == "true" ? true : false; }
            set { this["IsLocalService"] = value; }
        }

        /// <summary>
        /// 用户名.包括域
        /// </summary>
        [ConfigurationProperty("User", IsRequired = false)]
        public string User
        {
            get { return this["User"] as string; }
            set { this["User"] = value; }
        }

        /// <summary>
        /// 密码
        /// </summary>
        [ConfigurationProperty("Password", IsRequired = false)]
        public string Password
        {
            get { return this["Password"] as string; }
            set { this["Password"] = value; }
        }

        public Server(string Host, int Port)
        {
            this.Host = Host;
            this.Port = Port;
        }
        public Server() { }
    }

    public class ErrorServer : Server
    {
        public DateTime CheckTime;
    }
}
